# -*- coding: utf-8 -*-

"""
	BpNN_tensorflow test file
"""
import normalization_method
import BpNN_tensorflow as Bt
import numpy as np 

def genegrate_data(file_path):
	"""
		Generate data for BpNN
	"""
	feature_vector = []; label_vector = []
	with open(file_path, 'r') as file:
		for line in file.readlines():
			splitd_data = line.strip().split(',')
			feature_vector.append([float(x) for x in splitd_data[: -1]])
			label_vector.append([int(splitd_data[-1])])
		# print(feature_vector, label_vector)
	return np.array(feature_vector), np.array(label_vector)

def calculate_accuracy(pre_label, true_label):
	"""
		Caluclate the accuracy of the BpNN

		Args: 
			pre_label: the predict label list
			true_label: the true label list

		Returns:
			the accuracy
	"""
	correct_num = 0
	for i in range(pre_label.shape[0]):
		
		
		if (pre_label[i][0] > (true_label[i][0] - 0.5)) and \
		   (pre_label[i][0] < (true_label[i][0] + 0.5)):
			correct_num += 1
		else:
			print(pre_label[i][0], true_label[i][0])
	return correct_num / float(pre_label.shape[0])

if __name__ == '__main__':
	feature_vector, label_vector = genegrate_data('./data/train.txt')
	feature_vector_nor = normalization_method.zero_one(feature_vector)
	feature_test, label_test = genegrate_data('./data/test.txt')
	feature_test_nor = normalization_method.zero_one(feature_test)
	nn = Bt.BpNeuralNetwork()
	pre_label = nn.train(feature_vector_nor, feature_vector_nor.shape[1], 10, label_vector, 1, limit = 10000)
	print("train accuracy:%.2f" % calculate_accuracy(pre_label, label_vector))
	test_pre_label = nn.predict(feature_test_nor)
	print("test accuracy:%.2f" % calculate_accuracy(test_pre_label, label_test))
